﻿using Microsoft.Office.Tools.Word;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Office = Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;

namespace VSTOWord
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Application.WindowBeforeDoubleClick += Application_WindowBeforeDoubleClick;
        }

        private void Application_WindowBeforeDoubleClick(Word.Selection Sel, ref bool Cancel)
        {
            try
            {
                if (Sel == null) return;

                bool hasInlinePicture = Sel.InlineShapes != null && Sel.InlineShapes.Count > 0;
                bool hasShapePicture = false;
                try { hasShapePicture = Sel.ShapeRange != null && Sel.ShapeRange.Count > 0; } catch { }

                if (!(hasInlinePicture || hasShapePicture)) return;

                Cancel = true;

                // 1) 记录拷贝前的剪贴板
                IDataObject prevClipboard = null;
                try { prevClipboard = Clipboard.GetDataObject(); } catch { }

                // 2) 复制选区为图片
                Sel.CopyAsPicture();

                // 3) 从剪贴板取图并克隆成独立位图（与 Word/剪贴板解耦）
                Image previewImage = null;
                if (Clipboard.ContainsImage())
                {
                    var img = Clipboard.GetImage();
                    if (img != null)
                    {
                        previewImage = new Bitmap(img);
                    }
                }

                // 4) 立即覆盖/恢复剪贴板，避免退出 Word 时提示
                //try
                //{
                //    if (prevClipboard != null)
                //    {
                //        // 持久化恢复，避免需要源进程存活
                //        Clipboard.SetDataObject(prevClipboard, true);//使用后比较卡4-5秒
                //    }
                //    else
                //    {
                //        // 或者放入你自己的小文本/空内容
                //        Clipboard.Clear();
                //        // Clipboard.SetText(string.Empty);
                //    }
                //}
                //catch { }

                // 5) 显示预览（使用我们克隆的位图）
                if (previewImage != null)
                {
                    var viewer = new FrmImageViewer(previewImage);
                    viewer.StartPosition = FormStartPosition.CenterScreen;
                    viewer.Show();
                }
                Clipboard.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            try
            {
                this.Application.WindowBeforeDoubleClick -= Application_WindowBeforeDoubleClick;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region VSTO 生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
